<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<link rel="stylesheet" href="css/style.css" />
		<script src="https://cdn.bootcss.com/jquery/2.1.0/jquery.min.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript" src="laydate/laydate.js" ></script>
		<script type="text/javascript" src="js/js.js" ></script>
		<script src="../js/ajaxFunction.js" type="text/javascript" charset="utf-8"></script>
		<script>
			$(function(){
				page = 1; // 当前页数
				totalpage = 1;// 总页数
				getTaskInfo(1);
			});
			function getTaskInfo(spage){
				page = spage;
				page = page > totalpage ? totalpage : page;
				page = page < 1 ? 1 : page;
				$.ajax({
					url : '../TaskInfoServlet',
					method : 'post',
					dataType : 'json',
					data : {
						'condition' : 'alltask', 
						'page' : page, 
						'startTime' : $('#beginDate').val(), 
						'endTime' : $('#endDate').val(),
						'taskno' : $('#taskno').val(),
						'transportStatus' : $('#transportStatus').val(),
						'payno' : $('#payno').val(),
						'payStatus' : $('#payStatus').val(),
						'applyStatus' : $('#applyStatus').val(),
						'lackStatus' : $('#lackStatus').val(),
						'addressee' : $('#addressee').val()
						},
					success : function(tasks) {
						var str = "<tr>" + 
						"				<th><input type=\"checkbox\" onclick=\"swapCheck()\"/></th>" + 
						"				<th>下单时间</th>" + 
						"				<th>订单号</th>" + 
						"				<th>收件人</th>" + 
						"				<th>商品</th>" + 
						"				<th>订单金额</th>" + 
						"				<th>总价</th>" + 
						"				<th>付款编号</th>" + 
						"				<th>状态信息</th>" + 
						"			</tr> ";
						$.each(tasks, function(index,task,array){
							if (task.total == null){
								var oldTime = (new Date(task.time)).getTime();
							    var curTime = new Date(oldTime).Format("yyyy-MM-dd");
								str += "<tr class=\"tr_vertical_align\">" + 
								"					<td class=\"tb_tr_center\"><input type=\"checkbox\" name='taskno' value='"+task.no+"'/></td>" + 
								"					<td class=\"tb_tr_center\">"+curTime+"</td>" + 
								"					<td class=\"tb_tr_center\">"+task.no+"</td>" + 
								"					<td class=\"tb_tr_center\">"+task.user.username+"</td>" + 
								"					<td><img width='100' height='100' src=\"../"+task.goods.pic+"\" class=\"pic100 fl\" />" + 
								"				<div class=\"fl ml10\">" + 
								"					名称 : "+task.goods.name+"<br />"+
								"					品牌 : "+task.goods.brand+"<br /> 型号 : "+task.goods.type+" <span class=\"blue_span\"><br />" + 
								"					颜色 : "+task.goods.color+" <br /> 尺码 : <span class=\"blue_span\">"+task.goods.size+"</span> <br /> 数量 : "+task.goodsNumber+" <br />" + 
								"				</div></td>" + 
								"					<td class=\"tb_tr_center\">" + 
								"						<div class=\"ml10\">" + 
								"							原价 : "+task.goods.price+"</span> " + 
								"							<div>国际运费 : <span>"+task.freight+"</span></div><div>单价 : <span>"+task.price+"</span></div>";
								var row = 0;
								for (var i = 0; index + i < tasks.length && tasks[index + i].payNo == task.payNo; i++)
									row++;
								if (index == 0 || index > 0 && tasks[index-1].payNo != task.payNo){
									str += "<td rowspan='"+row+"'>总价: <span class=\"red_span\">￥"+task.totalPrice+"</span>" + 
									"				</td><td rowspan='"+row+"'><br /> 付款编号:"+task.payNo+" <br />";
								}
								str += "			</td><td>";
								switch(task.transportStatus){
								case '1': str += "<span class=\"red_span\">未发货</span> <br />";break;
								case '2': str += "<span class=\"red_span\">在途</span>  <br />";break;
								case '3': str += "<span class=\"red_span\">已到货</span>  <br />";break;
								case '4': str += "<span class=\"red_span\">已发货</span>  <br />";break;
								}
								switch(task.payStatus){
								case '1': str += "未付款<br />";break;
								case '2': str += "已付款   <br />";break;
								case '3': str += "已退款  <br />";break;
								}
								switch(task.applyStatus){
								case '1': str += "未申请发货  <br />";break;
								case '2': str += "已申请发货  <br />";break;
								}
								switch(task.lackStatus){
								case '1': str += "不缺货  <br />";break;
								case '2': str += "缺货  <br />";break;
								}
								switch(task.isPayFreight){
								case '1': str += "国内运费已付  <br />";break;
								case '0': str += "国内运费未付  <br />";break;
								}
								str += "</td></tr>" ;
							}else{
								totalpage = task.total / 3;
								totalpage = totalpage > parseInt(totalpage) ? parseInt(totalpage) + 1 : totalpage;
								$('#total').empty();
								$('#total').append(task.total);
								$('#curpage').empty();
								$('#curpage').append(page);
								$('#totalpage').empty();
								$('#totalpage').append(totalpage);
							}
						})
						$('#taskTable').empty();
						$('#taskTable').append(str);//class='red_span'
						
					}
				});
			}
			function postOperation(operation){
				var tasknos = [];
				if ($("input[name=taskno]:checked").length == 0){
					alert('请先选择需要操作的订单！');
					return;
				}
				$("input[name=taskno]:checked").each(function (index, item){
					tasknos.push($(this).val());
				});
				$.ajax({
					url : '../TaskOperationServlet',
					method : 'post',
					data : {
						'operation' : operation,
						'tasknos' : tasknos
					},
					traditional:true,
					success : function(json) {
						alert('操作成功！');
						getTaskInfo(page);
					}
				});
			}
			function postDelivery(){
				var tasknos = [];
				if ($("input[name=taskno]:checked").length == 0){
					alert('请先选择需要操作的订单！');
					$('#closeButton').click();
					return;
				}
				$("input[name=taskno]:checked").each(function (index, item){
					tasknos.push($(this).val());
				});
				$.ajax({
					url : '../DeliveryOperationServlet',
					method : 'post',
					data : {
						'operation' : 'delivery',
						'tasknos' : tasknos,
						'address' : $('#addr').val(),
						'addressee' : $('#addressee').val(),
						'tel' : $('#tel').val()
					},
					traditional:true,
					success : function() {
						$('#closeButton').click();
						alert('操作成功！');
						getTaskInfo(page);
					}
				});
			}
			function fileUpload(){
				var ext = $('#excelFile').val();
				if (ext && /(xls|xlsx)$/.test(ext.toLowerCase())){
					var data = new FormData($('#excelForm')[0]);
					$.ajax({
						url : "../TaskExcelServlet",
						type : 'POST',
						data : data,
						dataType : 'JSON',
						cache : false,
						processData : false,
						contentType : false,
						success : function(json) {
							alert(json.message);
							getTaskInfo(page);
						}
					});
	            } else {  
	                alert("请上传格式为 xls | xlsx 的文件！");  
	            }
			}
		</script>
	</head>
	<body>
		<div class="main border-bottom-gray"><span class="fontBold"><a href="" class="mapsite">首页</a></span>&nbsp;/&nbsp;
		<span class="fontBold"><a href="javascript:void(0)" class="mapsite">订单管理</a></span>&nbsp;/&nbsp;订单信息</div>
		<div class="border-bottom-gray main">
			<form action="" method="post">
				<div class="padding10_0">
					<span class="fontBold fontsize12">订单日期选择 : </span><input type="text" class="input_text" placeholder="请开始选择日期" id="beginDate"/>&nbsp;
					<span  class="fontBold fontsize12">至</span>&nbsp;
					<input type="text" class="input_text" placeholder="请选择截至日期" id="endDate"/>&nbsp;&nbsp;
					<span class="fontBold fontsize12">订单编号 : </span><input id="taskno" type="text" class="input_text"  placeholder="请输入订单编号"/>&nbsp;&nbsp;
					<span class="fontBold fontsize12">付款编号 : </span><input id="payno" type="text" class="input_text"  placeholder="请输入付款编号"/>&nbsp;&nbsp;
				</div>
				<div class="padding10_0">
				<span class="fontBold fontsize12">收件人 : </span><input type="text" id="addressee"
					class="input_text" placeholder="请输入收件人" />&nbsp;&nbsp;
				<span class="fontBold fontsize12">发货状态 : </span> <select id="transportStatus" class="input_text">
					<option value="">全部</option>
					<option value="2">在途</option>
					<option value="3">已到货</option>
					<option value="1">未发货</option>
					<option value="4">已发货</option>
				</select>&nbsp;&nbsp; <span class="fontBold fontsize12">付款状态
					: </span><select class="input_text" id="payStatus">
					<option value="">全部</option>
					<option value="1">未付款</option>
					<option value="2">已付款</option>
					<option value="3">已退款</option>
				</select>&nbsp;&nbsp; <span class="fontBold fontsize12">领货状态
					: </span><select class="input_text" id="applyStatus">
					<option value="">全部</option>
					<option value="1">未领货</option>
					<option value="2">已领货</option>
				</select>&nbsp;&nbsp; <span class="fontBold fontsize12">缺货状态
					: </span><select class="input_text" id="lackStatus">
					<option value="">全部</option>
					<option value="1">不缺货</option>
					<option value="2">缺货</option>
				</select>&nbsp;&nbsp;
			</div>
				<input type="button" onclick="getTaskInfo(1)" class="input_submit" value="查询"/>&nbsp;&nbsp;
				<input type="button" value="添加订单" onclick="location='addOrders.html'" class="input_submit"/>&nbsp;&nbsp;
				<input type="button" value="批量插入" class="input_submit" onclick="$('#excelFile').click()"/>&nbsp;&nbsp;
				<a href="/template.xlsx" download="template.xlsx">下载模板</a>
			</form>
			<form id="excelForm" action="../TaskExcelServlet" method="post"  enctype="multipart/form-data">
				<input id="excelFile" accept="Excel文件|.xls,.xlsx" name="excelFile" type="file" onchange="fileUpload()" style="display:none;"/>
			</form>
		</div>
		<div class="main graybg">
			<form>
			<table class="tb_wid" id="taskTable">
			</table>
			<div class="fl padding10_0">
				<input type="button" onclick="postOperation('pay')" class="input_submit" value="已付款"/>
				<input type="button" onclick="postOperation('payback')" class="input_submit" value="已退款"/>
				<input type="button" onclick="postOperation('transportover')" class="input_submit" value="已到货"/>
				<input type="button" onclick="postOperation('payfreight')" class="input_submit" value="国内运费已付"/>
				<input type="button" onclick="postOperation('lack')" class="redbutton" value="缺货"/>
				<input type="button" onclick="postOperation('del')" class="redbutton" value="删除"/>
			</div>
			</form>
			<!-- <div align="left" valign="top" class="fenye">
				<span id="total"></span>条数据 <span id="curpage"></span>/<span id="totalpage"></span> 页&nbsp;&nbsp;
				<a href="javascript:void(0)" target="mainFrame" onfocus="this.blur()" onclick="getTaskInfo(1)">首页</a>&nbsp;&nbsp;
				<a href="javascript:void(0)" target="mainFrame" onfocus="this.blur()" onclick="getTaskInfo(--page)">上一页</a>&nbsp;&nbsp;
				<a href="javascript:void(0)" target="mainFrame" onfocus="this.blur()" onclick="getTaskInfo(++page)">下一页</a>&nbsp;&nbsp;
				<a href="javascript:void(0)" target="mainFrame" onfocus="this.blur()" onclick="getTaskInfo(totalpage)">尾页</a>
	        </div> -->
       	</div>
	</body>
	<script>
		laydate.render({
		  elem: '#endDate'
		});
		laydate.render({
		  elem: '#beginDate'
		});
	</script>
</html>
